Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[make:entity] fix multiple and nullable enums #1584

Merged
merged 1 commit into from
Aug 29, 2024

Conversation

Fan2Shrek
Copy link
Contributor

Fixes #1580
Fix: No use statement when enum are multiple

The problem was due to the first condition 'array' === $typeHint && !$nullable.
If both "multiple" and "nullable" were selected, $typeHint was overridden by the second condition.

@jrushlow jrushlow changed the title Fix 1580 - Mulitiple and Nullable enum [make:entity] fix multiple and nullable enums Jul 31, 2024
@jrushlow jrushlow added the Bug Bug Fix label Jul 31, 2024
Copy link
Collaborator

@jrushlow jrushlow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 minor details and we should be all set!

if (null !== $mapping->enumType) {
if ('array' === $typeHint) {
// still need to add the use statement
$this->addUseStatementIfNecessary($mapping->enumType);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can eliminate the else conditional in this if statement by moving addUserStatement...() outside and after `if ('array' === $typeHint) is called.

We're calling the add use statement method regardless if array === $typeHint correct?

Copy link
Contributor Author

@Fan2Shrek Fan2Shrek Jul 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're calling the add use statement method regardless if array === $typeHint correct?

We're calling it in both cases, but if array === $typeHint, we don't want the returned string; it's just to automate the use statement.

src/Util/ClassSourceManipulator.php Outdated Show resolved Hide resolved
Comment on lines +136 to 138
} else {
$typeHint = $this->addUseStatementIfNecessary($mapping->enumType);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For clarity to my suggestion above, removing that use statement call and moving this one outside of the if conditional would accomplish the same thing with less code if I'm not mistaken.

Suggested change
} else {
$typeHint = $this->addUseStatementIfNecessary($mapping->enumType);
}
}
$typeHint = $this->addUseStatementIfNecessary($mapping->enumType);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will override $typeHint with the enum type even if we use an array, which was the initial problem.
We don't want to use the returned type of addUseStatementIfNecessary if we specify an array.

@jrushlow jrushlow added the Status: Needs Work Additional work is needed label Jul 31, 2024
@jrushlow jrushlow added this to the v1.61.0 milestone Jul 31, 2024
Comment on lines +739 to +760
yield 'it_creates_a_new_class_with_enum_field_multiple_and_nullable' => [$this->createMakeEntityTest()
->run(function (MakerTestRunner $runner) {
$this->copyEntity($runner, 'Enum/Role-basic.php');

$runner->runMaker([
// entity class name
'User',
// add additional field
'role',
'enum',
'App\\Entity\\Enum\\Role',
// multiple
'y',
// nullable
'y',
// finish adding fields
'',
]);

$this->runEntityTest($runner);
}),
];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if this test is relevant

Copy link
Collaborator

@jrushlow jrushlow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @Fan2Shrek

@jrushlow jrushlow added Status: Reviewed Has been reviewed by a maintainer and removed Status: Needs Work Additional work is needed labels Aug 29, 2024
@jrushlow jrushlow merged commit 207ed9c into symfony:main Aug 29, 2024
8 checks passed
@jrushlow jrushlow mentioned this pull request Aug 29, 2024
@Fan2Shrek Fan2Shrek deleted the fix/1580 branch September 20, 2024 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Bug Fix Status: Reviewed Has been reviewed by a maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MakerBundle "enum" entity type prompts "Can this field store multiple enum values" but doesn't handle the case
2 participants